﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.MsSystemDoc;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Ccit.OA.SystemDoc
{
    public partial class SystemDoc_codemanage_tree : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this._hidClassType.Value = (null != Request.QueryString["classType"]) ? Request.QueryString["classType"].ToString() : "";
                initializePage();
            }
        }
        /// <summary>
        /// 初始化页面
        /// </summary>
        private void initializePage()
        {
            createTree();
            trvDepAddress.ExpandLevel = 1;
        }
        private void createTree()
        {
            /* c创建根节点 */
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            ///调用创建根节点方法
            createRootNode(ref oRoot);
            ///添加第一层节点
            trvDepAddress.Nodes.Add(oRoot);
            ///添加节点信息
            createSubNodes(ref oRoot);
        }
        /// <summary>
        /// 创建根节点
        /// </summary>
        /// <param name="oRoot"></param>
        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            oRoot.Text = "编号规则";
            oRoot.ImageUrl = "../images/toolbar/Folder_open.gif";
            oRoot.NavigateUrl = "";
            oRoot.Target = "";
            oRoot.ID = "0_0";
            //Bll = null;
        }
        /// <summary>
        /// 节点信息
        /// </summary>
        /// <param name="oRoot"></param>
        private void createSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            SystemDocBLL Bll = new SystemDocBLL();
            string strAddrXML = Bll.GetCodeRuleNodesMes(_hidClassType.Value);   // get one stair ast type 
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;
            XElement XeAddr = XElement.Parse(strAddrXML);
            string CodeID = "";
            string CodeNAME = "";
            string hasChild = "";
            var eAddrArray = from eAddr in XeAddr.Elements("ADDR") select eAddr;

            foreach (var eAddr in eAddrArray)
            {
                CodeID = eAddr.Element("CodeID").Value;
                CodeNAME = eAddr.Element("CodeNAME").Value;
                hasChild = eAddr.Attribute("HASCHILD").Value;
                oTreeNode = createTreeNode(CodeNAME, CodeID, hasChild);
                oRoot.Nodes.Add(oTreeNode);
            }
            XeAddr = null;
            Bll = null;
        }
        /// <summary>
        /// 创建节点信息
        /// </summary>
        /// <param name="addrName">节点名称</param>
        /// <param name="addrId">节点id</param>
        /// <param name="hasChild">子节点id</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        private Microsoft.Web.UI.WebControls.TreeNode createTreeNode(string CodeNAME, string CodeID, string hasChild)
        {
            Microsoft.Web.UI.WebControls.TreeNode oNode = new Microsoft.Web.UI.WebControls.TreeNode();

            oNode.Text = CodeNAME;
            oNode.NavigateUrl = "";
            oNode.ImageUrl = "../images/toolbar/filelogo.gif";
            oNode.Target = "";
            oNode.ID = CodeID + "_" + "0";
            oNode.Type = "root";
            if (hasChild == "1")
            {
                Microsoft.Web.UI.WebControls.TreeNode oSubNode = new Microsoft.Web.UI.WebControls.TreeNode();
                oNode.Nodes.Add(oSubNode);
            }
            return oNode;
        }
        #region Ajax Methods
        /// <summary>
        /// 新增节点信息get all sub dep addresses
        /// </summary>
        /// <param name="addrId"></param>
        /// <param name="nodeIndex"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string GetSubAddrsByUpAddrId(string sClassType, string nodeIndex, string expandSign, string levelStruct)
        {
            SystemDocBLL BLL = new SystemDocBLL();
            List<MsCodeRule> lstSec = BLL.GetCodeRuleNodes(sClassType);
            BLL = null;
            string strResult = "GetSubTypesByColumnId" + "_" + JavaScriptConvert.SerializeObject(lstSec) + "_" + nodeIndex + "_" + expandSign + "_" + levelStruct + "_" + sClassType;
            return strResult;
        }
        #endregion 
    }
}
